package com.beijing.cyou.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.beijing.cyou.model.Privilege;
import com.beijing.cyou.model.UserInfo;
import com.beijing.cyou.model.UserOnline;
import com.beijing.cyou.params.HelpParams;
import com.beijing.cyou.params.UserParams;
import com.beijing.cyou.service.UserHelpService;
import com.beijing.cyou.service.UserService;
import com.beijing.cyou.utils.CommonConstant;
import com.beijing.cyou.utils.NotNullHelps;

@Slf4j
@RestController
@Api(value = "help控制器", description = "其他帮助API接口")
public class HelpController {

	@Autowired
	private UserHelpService userHelpService ;
	
	@Autowired
	private UserService userService ;
	
	private String message;

	@ResponseBody
	@RequestMapping("/privilege/{userId}/{type}")
	@ApiOperation(value = "旧版-用户权限" , notes = "旧版-知识详情", httpMethod = "GET")
	public Privilege getUserPrivilege(@PathVariable Long userId, @PathVariable Integer type) {
		Privilege privilege = new Privilege();
		if (NotNullHelps.checkHaveNotNull(userId, type) == false) {
			message = CommonConstant.common_message_exception;
		} else {
			HelpParams params = new HelpParams(userId, type);
			privilege = userHelpService.getUserPrivilege(params);
			message = "%% base-用户权限查询完毕！ @@";
		}
		log.info(message);
		return privilege;
	}
	
	@ResponseBody
	@RequestMapping("/userRedis")
	@ApiOperation(value = "用户redis详情" , notes = "由唯一token查询", httpMethod = "GET")
	public UserInfo setUserRedis(@RequestParam(value = "token") String token,String useremail, 
			String username , String domain , String usergender ,  String usercomment,
			@RequestParam(value = "userID",required = false) Long userID)throws Exception {
		UserInfo userInfo = null;
		if (NotNullHelps.checkNotNull(username, useremail, domain, usergender, usercomment) == false) {
			message = CommonConstant.common_message_exception;
		}else {
			UserParams condition = new UserParams(username,useremail,domain,usergender,usercomment,token);
			UserOnline userOnline = condition.changToUserOnline();
			if (NotNullHelps.isNotNull(userID)) {
				userOnline.setUserId(userID);
			}else {
				Long userId = userService.getUserIdByUserEmail(useremail) ;
				if (NotNullHelps.isNotNull(userId)) {
					userOnline.setUserId(userId);
				}
			}
			Long userId = userOnline.getUserId();
			if (NotNullHelps.isNotNull(userId) == false) {
				message = "## 用户暂无！@@" ;
			}else {
				userInfo = userService.getUserInfoById(userId);
				if (NotNullHelps.isNotNull(userInfo) == false) {
					message = "@@ 异常——用户信息！ %%" ;
				}else {
					userService.markupRedisUser(userOnline);
					message = "** 用户信息查询成功！ ##" ;
					userInfo.setUserToken(token);
				}
			}
		}
		log.info(message);
		return userInfo ;
	}

}
